"""
{machine:[source:[],..]..}
data set:{machines:[{machine_name:name1,sources},{},],property1:value1,property2:value2}
"""
import os
import pickle
import time


class DataDict:
    def __init__(self):
        self.root_path = "../../data"
        self.data = dict()

    def add_data_set(self, name, owner, description, collaborators=[], is_public=False, version_id=0):
        update_time = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        dataset = dict()
        dataset['name'] = name
        dataset['owner'] = owner
        dataset['description'] = description
        dataset['update_time'] = update_time
        dataset['collaborators'] = collaborators
        dataset['is_public'] = is_public
        dataset['version_id'] = version_id
        self.data[name] = dataset

    def load_from_config(self):
        exact_path = self.root_path + "/" + "datasets.data"
        if not os.path.exists(exact_path):
            self.save_to_config()
            print("initialize datasets.data")
        with open(exact_path, 'rb') as f:
            object_set = pickle.load(f)
        return object_set

    def save_to_config(self):
        exact_path_dir = self.root_path + "/"
        exact_path = self.root_path + "/" + "datasets.data"
        if not os.path.exists(exact_path_dir):
            os.mkdir(exact_path_dir)
        with open(exact_path, 'wb') as f:
            pickle.dump(self, f)


class DataSet:
    def __init__(self, name, owner="", description="", collaborators=[], is_public=False, version_id=0):
        self.machines = {}
        self.root_path = "../../data"
        self.set_name = name
        self.description = description
        self.owner = owner
        self.collaborators = collaborators
        self.is_public = is_public
        self.version_id = version_id

    def add_machine(self):
        pass

    def load_from_config(self):
        exact_path = self.root_path + "/" + self.set_name + "/" + self.set_name + ".data"
        print(exact_path)
        with open(exact_path, 'rb') as f:
            object_set = pickle.load(f)
        return object_set

    def save_to_config(self):
        dt = DataDict()
        dt = dt.load_from_config()
        dt.add_data_set(self.set_name, self.owner, self.description, self.collaborators, self.is_public,
                        self.version_id)
        dt.save_to_config()

        exact_path_dir = self.root_path + "/" + self.set_name
        exact_path = self.root_path + "/" + self.set_name + "/" + self.set_name + ".data"
        if not os.path.exists(exact_path_dir):
            os.mkdir(exact_path_dir)
        with open(exact_path, 'wb') as f:
            pickle.dump(self, f)


class Machine:
    def __init__(self, name, description="", host_ip="", offset=None):
        self.sources = {}
        self.host_name = name
        self.host_ip = host_ip
        self.offset = offset
        self.description = description

    def modify(self, name, description="", host_ip="", offset=None):
        self.host_name = name
        self.host_ip = host_ip
        self.offset = offset
        self.description = description


class Source:
    def __init__(self, name, description='', freq=1000, update_interval=24):
        self.columns = {}
        self.source_name = name
        self.description = description
        self.freq = 1000  # 1000毫秒为一个时间单元
        self.update_interval = 24  # 24小时更新一次


class Column:
    name: str
    dataset_name: str  # 属于哪个dataset
    host_name: str  # 属于哪个host
    source_name: str  # 属于哪个source
    type: str  # 'string'|'number'|'bool'|'int'

    # annotation: str  # 'start'|'end'|'edge'

    def __init__(self, col_name, dataset_name='', host_name='', source_name='', type='', annotation=""):
        self.name = col_name
        self.dataset_name = dataset_name
        self.host_name = host_name
        self.source_name = source_name
        self.type = type
        self.annotation = annotation


class UserDict:
    def __init__(self):
        self.root_path = "../../data"
        self.data = dict()

    def add_user(self, username, password):
        self.data[username] = password

    def load_from_config(self):
        exact_path = self.root_path + "/" + "users.data"
        if not os.path.exists(exact_path):
            self.save_to_config()
            print("initialize users.data")
        with open(exact_path, 'rb') as f:
            object_set = pickle.load(f)
        return object_set

    def save_to_config(self):
        exact_path_dir = self.root_path + "/"
        exact_path = self.root_path + "/" + "users.data"
        if not os.path.exists(exact_path_dir):
            os.mkdir(exact_path_dir)
        with open(exact_path, 'wb') as f:
            pickle.dump(self, f)


if __name__ == '__main__':
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
